<?php
/**
 * This is an unofficial PHP Plurk API provided by Ryan Lim.
 *
 * PHP version 5
 *
 * LICENSE: Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met: Redistributions of source code must retain the
 * above copyright notice, this list of conditions and the following
 * disclaimer. Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
 * NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 * 
 * @category  API 
 * @package   RLPlurkAPI
 * @author    Ryan Lim <plurk-api@ryanlim.com>
 * @copyright 2008 Ryan Lim
 * @license   http://www.opensource.org/licenses/bsd-license.php BSD License
 * @link      http://plurk.ryanlim.com/
 */

require_once dirname(__FILE__).'/../RLPlurkAPI.php';

class RLPlurkAPI_User extends RLPlurkAPI
{
    public $uid;
    
    public $nick_name;
    
    /**
     * Get a plurk user object!
     * 
     * <code>
     * <?php
     * $user = new RLPlurkAPI_User('saltybeagle');
     * echo $user->uid.' is '.$user->nick_name;
     * </code>
     *
     * @param string $nick_name
     */
    function __construct($nick_name)
    {
        parent::__construct();
        $this->http_client->get("http://www.plurk.com/user/{$nick_name}");
        $array_profile = $this->http_client->currentResponse();
        $this->parseHTMLResponse($array_profile['body']);
        $this->nick_name = $nick_name;
    }
    
    /**
     * Intended to parse a public plurk user page and retrieve useful info 
     * from it.
     *
     * @param string $html The html for the plurk user page.
     */
    protected function parseHTMLResponse($html)
    {
        preg_match('/var GLOBAL = \{.*"uid": ([\d]+),.*\}/imU',
            $html, $matches);
        $this->uid       = $matches[1];
    }
    
    /**
     * Return user's nick name.
     *
     * @return string Nick name!
     */
    public function __toString()
    {
        return $this->nick_name;
    }
}
